# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=subunit
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.4.0
pkgrel=1
pkgdesc="Python implementation of subunit test streaming protocol (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://launchpad.net/subunit'
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-extras"
         "${MINGW_PACKAGE_PREFIX}-python-testtools")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-fixtures"
              "${MINGW_PACKAGE_PREFIX}-python-hypothesis" 
              "${MINGW_PACKAGE_PREFIX}-python-testscenarios")
options=('staticlibs' 'strip' '!debug')
source=("https://pypi.io/packages/source/p/python-subunit/python-subunit-${pkgver}.tar.gz")
sha512sums=('7f8ebf1cfdd7ff22bfd68367cb90dd75e7f3b5f7da71830b304225905e1799ad1ec8f0d41f5f98895c831dc10fe77229137a472b8e8e57ea84c05c66694a5b5b')

# Helper macros to help make tasks easier #

prepare() {
  cd "${srcdir}"
  sed -i 's|import unittest2 as unittest|import unittest|' python-subunit-${pkgver}/python/subunit/tests/test_test_protocol.py

  rm -rf python-build-${CARCH} | true
  cp -r "python-${_realname}-${pkgver}" "python-build-${CARCH}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${CARCH}"  
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  PYTHONPATH="$PWD/build/lib:$PYTHONPATH" ${MINGW_PREFIX}/bin/python -m testtools.run subunit.test_suite || warning "Tests failed"
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  #fix so the correct interpretter is used
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for name in subunit-1to2 subunit-2to1 subunit-filter subunit-ls subunit-notify subunit-output subunit-stats subunit-tags subunit2csv subunit2gtk subunit2junitxml subunit2pyunit tap2subunit subunit2disk; do
    sed -e "s|${PREFIX_WIN}/bin/python.exe|${MINGW_PREFIX}/bin/python.exe|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/$name
  done
}
